<!doctype html>
<html lang="zh-CN">
	<head>
		<!-- 必须的 meta 标签 -->
		<meta charset="utf-8">
		<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no">

		<!-- Bootstrap 的 CSS 文件 -->
		<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/bootstrap@4.6.0/dist/css/bootstrap.min.css">
		<title>编辑</title>
		<style type="text/css">
			.loginPannel{
				width:500px;
				margin:30px auto; 
			}
			.loginPannel h3{text-align: center;}
		</style>
	</head>
	<body>
		<div class="loginPannel" id="app"> 
			<h3>编辑</h3>
			<form>
				<div class="form-group">
					<label for="exampleInputEmail1">用户名</label>
					<input type="text" class="form-control" v-model="form.userName" id="exampleInputEmail1">
					<small id="emailHelp" class="form-text text-muted"> </small>
				</div>
				<div class="form-group">
					<label for="exampleInputPassword1">性别</label>
					<div class="form-check form-check-inline">
					  <input class="form-check-input" type="radio" name="sex" id="inlineRadio1" value="true" v-model="form.sex">
					  <label class="form-check-label" for="inlineRadio1">男</label>
					</div>
					<div class="form-check form-check-inline">
					  <input class="form-check-input" type="radio" name="sex" id="inlineRadio2" value="false" v-model="form.sex">
					  <label class="form-check-label" for="inlineRadio2">女</label>
					</div>
				</div>
				<button type="button" class="btn btn-primary" @click="update">修改</button>
			</form>
		</div>
 
		<script src="https://cdn.jsdelivr.net/npm/jquery@3.5.1/dist/jquery.min.js"></script>
		   
		<script src="https://cdn.jsdelivr.net/npm/vue@2.6.12"></script>
		<script type="text/javascript">
			var app2 = new Vue({
				el: '#app',
				data: {
					id:'',
					form:{
						userName:'',
						sex:''
					} 
				},
				mounted() {
					this.id=location.search.split("=")[1]; 
					this.getUserById(this.id);
				},
				methods:{
					update(){
						 $.ajax({
							 headers: { 'token': localStorage.getItem('token')},
								url:'/api/user/'+this.id,
								type:'PUT',
								dataType:'json',
								data:this.form,
								success:function(data,status){   
									console.log(JSON.stringify(data),status);
										if(data.code==200)alert("修改成功")      
										location.href="./userList.html"
								} 
							}); 
					},
					
					getUserById(id){
						$.ajax({
							url:'/api/user/'+id,
							headers: { 'token': localStorage.getItem('token')},
							type:'GET',
							dataType:'json', 
							success:(data,status)=>{   
								console.log(data.data.name);
								this.form={
									userName:data.data.name,
									sex:data.data.gender,
								}
								this.$forceUpdate();
							} 
						}); 
						
					}
				}
			})
		</script>
	</body>
</html>
